package cn.ven.service;

import cn.ven.entity.Largefile;
import cn.ven.mapper.LargefileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
public class LargeFileServiceImpl  implements LargeFileService {

    @Autowired(required = false)
    LargefileMapper largefileMapper;


    //删除热图信息
    public void delLarge(String id){
        largefileMapper.delLarge(id);
    }

    //根据id查找热图信息
    public Largefile findById(String id){
        Largefile largefile = largefileMapper.findById(id);
        return largefile;
    }

    //添加图片信息
    @Override
    public String add(MultipartFile file) throws Exception {
        if (file!=null&&file.getSize()>0){//判断是否已经有了图片，如果有就更新
            Largefile largefile =new Largefile();
            String filename = file.getOriginalFilename();
            largefile.setFilename(filename);
            byte[] content = file.getBytes();//获取内容
            largefile.setContent(content);
            String id =UUID.randomUUID().toString();//获取唯一名
            largefile.setId(id);
            largefileMapper.addLargefile(largefile);
            return id;
        }
        //返回null的时候，认为角色没有图片上传
        return null;
    }

}
